﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kiel.Pdf
{
	/// <summary>
	/// Represents a Name Object.
	/// </summary>
	class PdfName : IPdfObject
	{
		/// <summary>
		/// Contains all the name objects created. This ensures that name objects with the same name value are pointing to the same reference.
		/// </summary>
		private readonly static Dictionary<string, PdfName> names = new Dictionary<string, PdfName>();

		private PdfName(string name)
		{
			if (name == null)
				throw new ArgumentNullException("name");
			this.Name = name;
		}

		public string Name { get; private set; }

		public static PdfName Create(string name)
		{
			lock (PdfName.names)
			{
				if (PdfName.names.ContainsKey(name))
					return PdfName.names[name];
				else
				{
					PdfName pdfname = new PdfName(name);
					PdfName.names[name] = pdfname;
					return pdfname;
				}
			}
		}

		public override bool Equals(object obj)
		{
			if (obj == null)
				return false;

			PdfName pdfname = obj as PdfName;
			if (pdfname == null)
				return false;

			return this.Name == pdfname.Name;
		}

		public override int GetHashCode()
		{
			return this.Name.GetHashCode();
		}

		public override string ToString()
		{
			return string.Format("/{0}", this.Name);
		}
	}
}
